package algocasts;

import java.util.HashMap;
import java.util.Map;

public class P2_towSum {
    public int[] twoSum_(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++)
                if (nums[i] + nums[j] == target)
                    return new int[]{i, j};
        }
        return new int[]{-1, -1};
    }

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int needNum = target - nums[i];
            if (map.containsKey(needNum))
                return new int[]{map.get(needNum), i};
            map.put(nums[i], i);
        }
        return new int[]{-1, -1};
    }

}
